package com.kizuki.thread;

public class Race implements Runnable{

    private static String winner;
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
            if (isWin(i)) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "跑了" + (i + 1) + "步");
        }
    }

    public Boolean isWin(int i) {
        if (winner != null) {
            return true;
        } else {
            if (i == 100) {
                winner = Thread.currentThread().getName();
                System.out.println(Thread.currentThread().getName() + "赢了");
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println("跑步比赛, 第一个参赛者跑了100步就结束");
        Race race = new Race();
        new Thread(race, "A").start(); // 内部实现是静态代理模式，Thread和Race都要继承Runnable
        new Thread(race, "B").start();

    }

}


